﻿unit UAutoTree;

interface

  uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, UBaseTemplate, DBGridEhGrouping, ToolCtrlsEh, DBGridEhToolCtrls, DynVarsEh,
    MemTableDataEh, Data.DB, MemTableEh, DataDriverEh, EhLibVCL, GridsEh, DBAxisGridsEh, DBGridEh, Vcl.ComCtrls,
    Vcl.ToolWin, System.ImageList, Vcl.ImgList, System.Actions, Vcl.ActnList, Vcl.ExtCtrls, Vcl.DBActns, USingle,
    FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf,
    FireDAC.DApt.Intf, FireDAC.Stan.Async, FireDAC.DApt, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
    frxClass, cxClasses,
    frxDBSet, Vcl.Menus, Data.Win.ADODB, cxtextedit, cxcheckbox, cxGridTableView, cxlabel, cxDBEdit,
    cxGridDBTableView, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters, cxCustomData, cxStyles, cxTL,
    cxTLdxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters, cxInplaceContainer, cxTLData, cxDBTL;

  type
    TfrmAtuoTree = class(TfrmSingle)
      DataSetDriverEh1: TDataSetDriverEh;
      MemTableEh1: TMemTableEh;
      DataSource1: TDataSource;
      Panel1: TPanel;
      Splitter1: TSplitter;
      Panel2: TPanel;
      FlowPanel1: TFlowPanel;
      FDQryModuleMasterSet: TFDQuery;
      cxDBTreeList1: TcxDBTreeList;
      procedure FormShow(Sender: TObject);
      procedure cxDBButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
      procedure cxDBTreeList1CustomDrawIndicatorCell(Sender: TcxCustomTreeList; ACanvas: TcxCanvas;
        AViewInfo: TcxTreeListIndicatorCellViewInfo; var ADone: Boolean);
    private
      { Private declarations }
    public
      { Public declarations }
    end;

  var
    frmAtuoTree: TfrmAtuoTree;

implementation

  Uses UDM, UMain, Ucommon, USelectdate, Uselectdata;

{$R *.dfm}

  procedure TfrmAtuoTree.cxDBButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  var
    curFieldName: String;
  begin
    inherited;
    // 在新增修改编辑状态下才可  弹出选择窗口
    if FDQryMaster.State in dseditmodes then
    begin
      // 主表选择内容通用事件
      curFieldName := TcxDBTextEdit(Sender).DataBinding.Field.FieldName;
      // 关联了数据源字段才可进行选择
      if curFieldName <> '' then
      begin
        FDQryModuleFields.Filtered := false;
        if FDQryModuleFields.Locate('MDF_QueryName;MDF_FieldName', vararrayof(['master', curFieldName]), []) then
        begin
          EnterSelectData(FDQryMaster, FDQryModuleFields.FieldByName('MDF_SelectSQL').AsString,
            FDQryModuleFields.FieldByName('MDF_SelectField').AsString);
        end;
      end;
    end;
  end;

  procedure TfrmAtuoTree.cxDBTreeList1CustomDrawIndicatorCell(Sender: TcxCustomTreeList; ACanvas: TcxCanvas;
    AViewInfo: TcxTreeListIndicatorCellViewInfo; var ADone: Boolean);
  var
    AIndicatorViewInfo: TcxTreeListIndicatorCellViewInfo;
    ATextRect: TRect;
    aCV: TcxCanvas;
  begin
    inherited;
    if not(AViewInfo is TcxTreeListIndicatorCellViewInfo) then
      Exit;
    aCV := ACanvas;
    ATextRect := AViewInfo.TextBounds;
    AIndicatorViewInfo := AViewInfo as TcxTreeListIndicatorCellViewInfo;
    InflateRect(ATextRect, -2, -1);

    if AIndicatorViewInfo.Node = nil then
      Exit;
    Sender.LookAndFeel.Painter.DrawHeader(ACanvas, AViewInfo.TextBounds, ATextRect, [], cxBordersAll, cxbsNormal,
      taCenter, TcxAlignmentVert(vaCenter), false, false, IntToStr(AIndicatorViewInfo.Node.AbsoluteIndex + 1), aCV.Font,
      aCV.Font.Color, aCV.Brush.Color);
    ADone := True;
  end;

  procedure TfrmAtuoTree.FormShow(Sender: TObject);
  var
    i: Integer;
    tmpfield: TField;
    tmpGridDBColumn: TcxGridDBColumn;
    tmpPanel: TPanel;
    tmplabel: TcxLabel;
    tmpinput: TcxCustomTextEdit;
    tmpcheckbox: TcxCustomCheckBox;
  begin
    inherited;

    // 读取主从数据源固态化字段内容
    FDQryModuleQuery.Filtered := false;
    FDQryModuleQuery.Filter := 'mqy_type=''master''';
    FDQryModuleQuery.Filtered := True;

    if FDQryModuleQuery.RecordCount <> 1 then
    begin
      showmessage('系统后台数据设置错误，请与管理员联系！');
      Exit;
    end
    else
    begin
      // FDQryinitialization.close;
      // FDQryinitialization.SQL.Text :='select * from ('+ FDQryModuleQuery.FieldByName('mqy_sql').AsString +') a where 1<>1';
      // FDQryinitialization.Open();
      FDQryMaster.close;
      FDQryMaster.SQL.Text := FDQryModuleQuery.FieldByName('mqy_sql').AsString;
      FDQryMaster.Open();
    end;

    // FDQryDetail.Fields.Clear ;
    // FDQryDetail.CopyFields(FDQryinitialization); // 复制从表动态字段

    // 主表输入数据源
    FDQryModuleMasterSet.close;
    FDQryModuleMasterSet.ParamByName('mod_code').Value := Name;
    FDQryModuleMasterSet.Open();
    FDQryModuleMasterSet.First;

    FlowPanel1.Caption := '';
    while not FDQryModuleMasterSet.Eof do
    begin
      // TcxDBTextEdit   cxDBedit   cxTextEdit
      // TcxDBMemo       cxMeno
      // TcxDBMaskEdit    cxMaskEdit
      // TcxDBButtonEdit  cxButtonEdit
      // TcxDBComboBox ,TcxDBPopupEdit   cxDropDownEdit
      // TcxDBSpinEdit  上下增加数据  cxSpinEdit
      // TcxDBTimeEdit   cxTimeEdit
      // TcxDBBlobEdit   cxBlobEdit
      tmpPanel := TPanel.Create(FlowPanel1);
      tmpPanel.Parent := FlowPanel1;
      tmpPanel.Name := 'Master_pnl_' + FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
      tmpPanel.Caption := '';
      tmpPanel.Height := 44;
      tmpPanel.AlignWithMargins := True;
      tmpPanel.BevelOuter := bvNone; // 无边线
      if UPPERCASE(FDQryModuleMasterSet.FieldByName('MMS_Type').AsString) <> 'CHECK' then
      begin
        tmplabel := TcxLabel.Create(tmpPanel);
        tmplabel.Parent := tmpPanel;
        tmplabel.Name := 'Master_label_' + FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        tmplabel.Caption := FDQryModuleMasterSet.FieldByName('MMS_Displaylabel').AsString;
        tmplabel.Align := altop;
        tmplabel.Transparent := True; // label透明，显示背景底色
      end;
      if UPPERCASE(FDQryModuleMasterSet.FieldByName('MMS_Type').AsString) = 'TEXT' then
      begin
        tmpinput := TcxDBTextEdit.Create(tmpPanel);
        tmpinput.Parent := tmpPanel;
        tmpinput.Name := 'DBedit_' + FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        tmpinput.Align := albottom;
        TcxDBTextEdit(tmpinput).DataBinding.DataField := FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        TcxDBTextEdit(tmpinput).DataBinding.DataSource := dsMaster;
        if (not FDQryModuleMasterSet.FieldByName('MMS_CanInput').asboolean) or
          (IfAutoNumber and (UPPERCASE(FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString)
          = UPPERCASE(StrKeyField))) then
        begin
          TcxDBTextEdit(tmpinput).Properties.ReadOnly := True;
          TcxDBTextEdit(tmpinput).Style.Color := clInactiveCaption;
        end;
        tmplabel.FocusControl := tmpinput;

      end;
      if UPPERCASE(FDQryModuleMasterSet.FieldByName('MMS_Type').AsString) = 'BUTTON' then
      begin
        tmpinput := TcxDBButtonEdit.Create(tmpPanel);
        tmpinput.Parent := tmpPanel;
        tmpinput.Name := 'DBedit_' + FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        tmpinput.Align := albottom;
        TcxDBButtonEdit(tmpinput).DataBinding.DataField := FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        TcxDBButtonEdit(tmpinput).DataBinding.DataSource := dsMaster;
        TcxDBButtonEdit(tmpinput).Properties.ReadOnly := not FDQryModuleMasterSet.FieldByName('MMS_CanInput').asboolean;
        TcxDBButtonEdit(tmpinput).Properties.OnButtonClick := cxDBButtonEdit1PropertiesButtonClick;
        if TcxDBButtonEdit(tmpinput).Properties.ReadOnly then
          TcxDBButtonEdit(tmpinput).Style.Color := clInactiveCaption;
        tmplabel.FocusControl := tmpinput;
      end;
      if UPPERCASE(FDQryModuleMasterSet.FieldByName('MMS_Type').AsString) = 'DATE' then
      begin
        tmpinput := TcxDBDateEdit.Create(tmpPanel);
        tmpinput.Parent := tmpPanel;
        tmpinput.Name := 'DBedit_' + FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        tmpinput.Align := albottom;
        TcxDBDateEdit(tmpinput).DataBinding.DataField := FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        TcxDBDateEdit(tmpinput).DataBinding.DataSource := dsMaster;
        TcxDBDateEdit(tmpinput).Properties.ReadOnly := not FDQryModuleMasterSet.FieldByName('MMS_CanInput').asboolean;
        TcxDBDateEdit(tmpinput).Properties.DisplayFormat := FormatSettings.ShortDateFormat;
        TcxDBDateEdit(tmpinput).Properties.EditFormat := FormatSettings.ShortDateFormat;
        if TcxDBDateEdit(tmpinput).Properties.ReadOnly then
          TcxDBDateEdit(tmpinput).Style.Color := clInactiveCaption;
        tmplabel.FocusControl := tmpinput;
      end;

      if UPPERCASE(FDQryModuleMasterSet.FieldByName('MMS_Type').AsString) = 'CHECK' then
      begin
        tmpcheckbox := TcxDBCheckBox.Create(tmpPanel);
        tmpcheckbox.Parent := tmpPanel;
        tmpcheckbox.Name := 'DBedit_' + FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        tmpcheckbox.Align := albottom;
        TcxDBCheckBox(tmpcheckbox).Caption := FDQryModuleMasterSet.FieldByName('MMS_Displaylabel').AsString;
        TcxDBCheckBox(tmpcheckbox).DataBinding.DataField := FDQryModuleMasterSet.FieldByName('MMS_Fieldname').AsString;
        TcxDBCheckBox(tmpcheckbox).DataBinding.DataSource := dsMaster;
        TcxDBCheckBox(tmpcheckbox).Properties.ReadOnly := not FDQryModuleMasterSet.FieldByName('MMS_CanInput')
          .asboolean;
        if TcxDBCheckBox(tmpcheckbox).Properties.ReadOnly then
          TcxDBCheckBox(tmpcheckbox).Style.Color := clInactiveCaption;
        tmplabel.FocusControl := tmpcheckbox;
      end;

      FDQryModuleMasterSet.Next;
    end;


  end;

end.
